
//TrieNode构造函数
function TrieNode() {
  this.children = {}
  this.isEnd = false
}

var Trie = function () {
  this.root = new TrieNode()
};

/** 
 * @param {string} word
 * @return {void}
 */
Trie.prototype.insert = function (word) {
  let node = this.root
  for (let char of word) {
    if (!node.children[char]) {
      node.children[char] = new TrieNode()
    }
    node = node.children[char]
  }
  node.isEnd = true
};
//辅助原型函数：
Trie.prototype._searchPrefix = function (prefix) {
  let node = this.root
  for (let char of prefix) {
    if (!node.children[char]) {
      return null
    }
    node = node.children[char]
  }
  return node
}
/** 
 * @param {string} word
 * @return {boolean}
 */
Trie.prototype.search = function (word) {
  const node = this._searchPrefix(word)
  return node !== null && node.isEnd
};

/** 
 * @param {string} prefix
 * @return {boolean}
 */
Trie.prototype.startsWith = function (prefix) {
  const node = this._searchPrefix(prefix)
  return node !== null
};

/** 
 * Your Trie object will be instantiated and called as such:
 * var obj = new Trie()
 * obj.insert(word)
 * var param_2 = obj.search(word)
 * var param_3 = obj.startsWith(prefix)
 */